﻿using System;
using System.Collections.Generic;
using System.Linq;
using Apps.IBLL;
using Apps.Models;
using Apps.IDAL;
using Apps.DAL;
using Apps.Models.Sys;
using Unity.Attributes;
using Apps.Common;

namespace Apps.BLL
{
	public class SysSampleBLL : ISysSampleBLL
	{
		AppsDBEntities dBEntities = new AppsDBEntities();
		[Dependency]
		public ISysSampleRepository rep { get; set; }
		/// <summary>
		/// 获取数据列表
		/// </summary>
		/// <param name="queryStr">搜索条件</param>
		/// <returns></returns>
		public List<SysSampleModel> GetList(ref GridPager gridPager)
		{
			IQueryable<SysSample> queryData = null;
			queryData = rep.GetList(dBEntities);

			//排序
			if (gridPager.order == "desc")
			{
				switch (gridPager.order)
				{
					case "Id":
						queryData = queryData.OrderByDescending(c => c.Id);
						break;
					case "Name":
						queryData = queryData.OrderByDescending(c => c.Name);
						break;
					default:
						queryData = queryData.OrderByDescending(c => c.CreateTime);
						break;
				}
			}
			else
			{

				switch (gridPager.order)
				{
					case "Id":
						queryData = queryData.OrderBy(c => c.Id);
						break;
					case "Name":
						queryData = queryData.OrderBy(c => c.Name);
						break;
					default:
						queryData = queryData.OrderBy(c => c.CreateTime);
						break;
				}
			}
			return CreateModelList(ref gridPager, ref queryData);
		}


		private List<SysSampleModel> CreateModelList(ref GridPager pager, ref IQueryable<SysSample> queryData)
		{

			pager.totalRows = queryData.Count();
			if (pager.totalRows > 0)
			{
				if (pager.page <= 1)
				{
					queryData = queryData.Take(pager.rows);
				}
				else
				{
					queryData = queryData.Skip((pager.page - 1) * pager.rows).Take(pager.rows);
				}
			}
			List<SysSampleModel> modelList = (from r in queryData
											  select new SysSampleModel
											  {
												  Id = r.Id,
												  Name = r.Name,
												  Age = r.Age,
												  Bir = r.Bir,
												  Photo = r.Photo,
												  Note = r.Note,
												  CreateTime = r.CreateTime,

											  }).ToList();

			return modelList;
		}

		/// <summary>
		/// 创建一个实体
		/// </summary>
		/// <param name="sysSample"></param>
		/// <returns></returns>
		public bool Create(SysSampleModel sysSampleModel)
		{
			try
			{
				SysSample sysSample = rep.GetById(sysSampleModel.Id.ToString());
				if (sysSample != null)
				{
					return false;
				}
				sysSample = new SysSample();
				sysSample.Id = sysSampleModel.Id;
				sysSample.Name = sysSampleModel.Name;
				sysSample.Age = sysSampleModel.Age;
				sysSample.Bir = sysSampleModel.Bir;
				sysSample.Photo = sysSampleModel.Photo;
				sysSample.Note = sysSampleModel.Note;
				sysSample.CreateTime = sysSampleModel.CreateTime;
				if (rep.Create(sysSample) == 1)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch (Exception)
			{
				//ExceptionHandler.WriteException(ex);
				return false; ;
			}
		}
		/// <summary>
		/// 删除一个实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public bool Delete(string id)
		{
			try
			{
				if (rep.Delete(id) == 1)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch (Exception)
			{

				return false;
			}
		}
		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="deleteCollection">id集合</param>
		/// <returns>删除条数</returns>
		public int Delete(string[] deleteCollection)
		{
			try
			{
				int count = 0;
				foreach (var id in deleteCollection)
				{
					if (rep.Delete(id) == 1)
					{
						count++;
					}
				}
				return count;
			}
			catch (Exception)
			{
				throw;
			}
		}
		/// <summary>
		/// 编辑实体
		/// </summary>
		/// <param name="sysSample"></param>
		/// <returns></returns>
		public bool Edit(SysSampleModel sysSampleModel)
		{
			try
			{
				SysSample sysSample = rep.GetById(sysSampleModel.Id.ToString());
				if (sysSample == null)
				{
					return false;
				}
				sysSample.Name = sysSampleModel.Name;
				sysSample.Age = sysSampleModel.Age;
				sysSample.Bir = sysSampleModel.Bir;
				sysSample.Photo = sysSampleModel.Photo;
				sysSample.Note = sysSampleModel.Note;

				if (rep.Edit(sysSample) == 1)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch (Exception)
			{

				return false;
			}
		}
		/// <summary>
		/// 通过id获取实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns>实体</returns>
		public SysSampleModel GetById(string id)
		{
			if (IsExist(id))
			{
				SysSample sys = rep.GetById(id);
				SysSampleModel model = new SysSampleModel();
				model.Id = sys.Id;
				model.Name = sys.Name;
				model.Age = sys.Age;
				model.Bir = sys.Bir;
				model.Photo = sys.Photo;
				model.Note = sys.Note;
				model.CreateTime = sys.CreateTime;
				return model;
			}
			else
			{
				return new SysSampleModel();
			}
		}
		/// <summary>
		/// 判断实体是否存在
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public bool IsExist(string id)
		{
			return rep.IsExist(id);
		}
	}
}
